home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / Processes / LaunchWithDoc / LaunchWithDoc.c next >
Encoding:
C/C++ Source or Header  |  1994-11-11  |  5.0 KB  |  122 lines  |  [TEXT/MPS ]

  1. /* LaunchWithDoc */
  2. /* The smallest LaunchAplication example I could come up with. */
  3. /* this launches an application with a 'odoc' AppleEvent™ */
  4. /* this little bit of code here uses two Standard File calls to get */
  5. /* the application to launch and the file to launch with, you will of course */
  6. /* want to replace these with whatever you want to do */
  7. /* ••• NOTE: This also works for launching non-System 7 applications.  */
  8. /* If the Finder™ sees an 'odoc' or 'pdoc' Apple event in the launch block */
  9. /* and the application being launched is NOT system 7 aware, the Finder */
  10. /* coerces the Apple event into puppetstrings.  So you */
  11. /* don't need to special case for non-7.0 applications.  */
  12. /* Pretty neat, huh? */
  13. /* ••• Important: I'm not doing any error checking, you of course should */
  14. /* so your users don't hunt you down and burn your keyboard. */
  15. /* C.K. Haun */
  16. /* Apple DTS */
  17.  
  18. /* And another Note: */
  19. /* Please see the sample FinderOpenSel 1.0.1 for more information on opening/launching */
  20. /* applications and stuff.  Using the Finder event is much more versitile than */
  21. /* just using launchApplication. */
  22. #include <Dialogs.h>
  23. #include <QuickDraw.h>
  24. #include <Windows.h>
  25. #include <Menus.h>
  26. #include <Fonts.h>
  27. #include <appleevents.h>
  28. #include <processes.h>
  29. #include <files.h>
  30. #include <StandardFile.h>
  31. #include <Aliases.h>
  32.  
  33. #ifdef powerc
  34.    QDGlobals    qd;
  35. #endif
  36.  
  37. main()
  38. {
  39.     FSSpec launchApp, fileToLaunch;   /* file spec for the app (launchApp) and the file (fileToLaunch) */
  40.     OSErr myErr;
  41.     LaunchParamBlockRec launchThis;
  42.     AEDesc myAddress;
  43.     ProcessSerialNumber myPSN;
  44.     AEDesc docDesc, launchDesc;
  45.     AEDescList theList;
  46.     AliasHandle withThis;
  47.     StandardFileReply myReply;
  48.     AppleEvent theEvent;
  49.     InitGraf((Ptr)&qd.thePort);  /* standard setup stuff */
  50.     InitFonts();
  51.     InitWindows();
  52.     InitMenus();
  53.     TEInit();
  54.     InitDialogs(nil);
  55.     InitCursor();
  56.     /* get the app to launch, using the Sys7 versions of Standard File */
  57.     /* which return FSSpecs */
  58.     /* Preset our address descriptor to our PSN */
  59.     /* The Finder is going to change this to whatever is appropriate  */
  60.     /* for the application that eventually gets launched, so I'll */
  61.     /* just fill it with my own address. */
  62.     /* I'm doing this because I have heard sporadic reports that  */
  63.     /* _not_ doing this causes address errors sometimes, I've never had that happen. */
  64.     /* But since it has, you should prefill. */
  65.     GetCurrentProcess(&myPSN);
  66.     /* create the address desc for the event */
  67.     myErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&myPSN, sizeof(ProcessSerialNumber), &myAddress);
  68.     /* get the application to launch */
  69.     StandardGetFile(nil, -1, nil, &myReply);
  70.     if (!myReply.sfGood)
  71.         return;
  72.     launchApp = myReply.sfFile;
  73.     /* stuff it in my launch parameter block */
  74.     launchThis.launchAppSpec = &launchApp;
  75.     
  76.     /* get the file to pass */
  77.     StandardGetFile(nil, -1, nil, &myReply);
  78.     if (!myReply.sfGood)
  79.         return;
  80.     
  81.     /* the caller may have already done this, but it doesn't hurt to do it again */
  82.     fileToLaunch = myReply.sfFile;
  83.     /* create an appleevent to carry it */
  84.     AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &myAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  85.     /* create a list for the alaises.  In this case, I only have one, but you still need */
  86.     /* a list */
  87.     AECreateList(nil, 0, false, &theList);
  88.     /* create an alias out of the file spec */
  89.     /* I'm not real sure why I did this, since there is a system coercion handler for */
  90.     /* alias to FSSpec, but I'm paranoid */
  91.     NewAlias(nil, &fileToLaunch, &withThis);
  92.     HLock((Handle)withThis);
  93.     /* now create an alias descriptor */
  94.     AECreateDesc(typeAlias, (Ptr)*withThis, GetHandleSize((Handle)withThis), &docDesc);
  95.  
  96.     HUnlock((Handle)withThis);
  97.     /* put it in the list */
  98.     AEPutDesc(&theList, 0, &docDesc);
  99.     AEPutParamDesc(&theEvent, keyDirectObject, &theList);
  100.     /* coerce the event from being an event into being appParms */
  101.     /* •••• If you just want to send an 'odoc' to an application that is */
  102.     /* already running, you can stop here and do an AESend on theEvent */
  103.     AECoerceDesc(&theEvent, typeAppParameters, &launchDesc);
  104.     HLock((Handle)theEvent.dataHandle);
  105.     /* and stuff it in the parameter block */
  106.     /* This is a little weird, since we're actually moving the event out of the */
  107.     /* AppParameters descriptor.  But it's necessary, the coercison to typeAppParameters */
  108.     /* stuffs the whole appleevent into one AERecord (instead of a AEDesc) so  */
  109.     /* the Finder gets the whole event as one handle.  It can then parse it itself */
  110.     /* to do the sending */
  111.     launchThis.launchAppParameters = (AppParametersPtr)*(launchDesc.dataHandle);
  112.     /* launch the thing */
  113.     launchThis.launchBlockID = extendedBlock;
  114.     launchThis.launchEPBLength = extendedBlockLen;
  115.     launchThis.launchFileFlags = nil;
  116.     launchThis.launchControlFlags = launchContinue + launchNoFileFlags;
  117.     LaunchApplication(&launchThis);
  118.     /* and launch it */
  119.     
  120.     
  121. }
  122.